<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Sso{
	var $TBL_USER = 'users';
	var $_DB;
	var $_CI;
	var $current_db = "";
	
	var $user_id = 0;
	var $user_name = '';
	var $password = '';
	var $email = '';
	var $full_name =''; 	
	var $first_name = ''; 
	var $last_name = '';	
	var $doj = ''; 	
	var $country = '';
	var $cookie = ''; 	
	var $session = ''; 	
	var $ip = ''; 	
	var $signup_ip = ''; 	
	var $pref_lang = ''; 	
	var $hometown = '';
	var $city = ''; 	
	var $state = ''; 	
	var $zip = ''; 	
	var $theme = ''; 	
	var $mobile = ''; 	
	var $usr_status = 'Ok'; 	
	
	function __destruct() {
	}
	
	function __construct() {
		$this->_CI = &get_instance();
		 
		$db['local']['hostname'] = 'localhost';
		$db['local']['username'] = 'root';
		$db['local']['password'] = '';
		$db['local']['database'] = 'sunbiz';
		
		$db['live']['hostname'] = 'localhost';
		$db['live']['username'] = 'sunbizte';
		$db['live']['password'] = 'panguru005';
		$db['live']['database'] = 'sunbizte_iclclicxa';
		
		$this->current_db = mysql_connect($db[ENVIRONMENT]['hostname'],$db[ENVIRONMENT]['username'],$db[ENVIRONMENT]['password']); 
		if (!$this->current_db) {
			return false;
		}
 
		$this->_DB = mysql_select_db($db[ENVIRONMENT]['database'], $this->current_db);
		 
	}
	
	function init($cmd, $input){
		if(!$input || !$this->_DB){
			return false;
		}
		
		if($cmd == 'user_id')
		{
			$qr = "SELECT * FROM {$this->TBL_USER} WHERE user_id='$input'";
			$res = mysql_query($qr);
			$result = mysql_fetch_assoc($res);
		}
		else if($cmd == 'user_name')
		{
			$qr = "SELECT * FROM {$this->TBL_USER} WHERE user_name='$input'";
			$res = mysql_query($qr);
			$result = mysql_fetch_assoc($res);
		}
		else if($cmd == 'email')
		{
			$qr = "SELECT * FROM {$this->TBL_USER} WHERE email='$input'";
			$res = mysql_query($qr);
			$result = mysql_fetch_assoc($res);
		}else{
			return false;
		}
		
		if(empty($result)){return false;}
		
		$this->user_id = $result['user_id'];
		$this->user_name = $result['user_name'];
		$this->password = $result['password'];
		$this->email = $result['email'];
		$this->full_name = $result['full_name'];
		$this->first_name = $result['first_name'];
		$this->last_name = $result['last_name'];
		$this->doj = $result['doj'];
		$this->country = $result['country'];
		$this->cookie = $result['cookie'];
		$this->session = $result['session'];
		$this->ip = $result['ip'];
		$this->signup_ip = $result['signup_ip'];
		$this->pref_lang = $result['pref_lang'];
		$this->hometown = $result['hometown'];
		$this->city = $result['city'];
		$this->state = $result['state'];
		$this->zip = $result['zip'];
		$this->theme = $result['theme'];
		$this->mobile = $result['mobile'];
		$this->usr_status = $result['usr_status'];
		 
		return $this;
	}
	
	function setUserId($input){$this->user_id = $input;}
	function setUserName($input){$this->user_name = $input;}
	function setPassword($input){$this->password = $input;}
	function setEmail($input){$this->email = $input;}
	function setFullName($input){$this->full_name = $input;}
	function setDOJ($input){$this->doj=$input;}
	function setCityName($input){$this->city = $input;}
	function setCountryCode($input){$this->country = $input;}
	function setStateName($input){$this->state = $input;}
	function setMobileNo($input){$this->mobile_no = $input;}
	
	function getUserId(){ return $this->user_id ;}
	function getUserName(){ return $this->user_name ;}
	function getPassword(){ return $this->password ;}
	function getEmail(){ return $this->email;}
	function getFullName(){ return $this->full_name;}
	function getDOJ(){ return $this->doj;}
	function getCityName(){return $this->city;}
	function getCountryCode(){ return $this->country;}
	function getStateName(){ return $this->state;}
	function getMobileNo(){ return $this->mobile ;}
	function getStatus(){ return $this->usr_status ;}
	
	function createNewUser(){
		if(!$this->_DB){return false;}
		$qr = "INSERT INTO {$this->TBL_USER} 
					(`user_name`,`email`,`password`,`first_name`,`last_name`,
					`country`,`cookie`,`session`,`ip`,`signup_ip`,`pref_lang`,
					`hometown`,`city`,`zip`,`theme`	
					) ";
		$qr .= "VALUES ('','','','','','','','','','','','','','','')";
		$res = mysql_query($qr);
		return mysql_insert_id(); 
	}
	
	function update(){
		if(!$this->_DB){return false;}
		
		$qr = "UPDATE {$this->TBL_USER} SET ";
		$qr .= " user_name='{$this->user_name}', ";
		$qr .= " password='{$this->password}', ";
		$qr .= " email='{$this->email}', ";
		$qr .= " full_name='{$this->full_name}', ";
		$qr .= " first_name='{$this->first_name}', ";
		$qr .= " last_name='{$this->last_name}', ";
		$qr .= " doj='{$this->doj}', ";
		$qr .= " country='{$this->country}', ";
		$qr .= " cookie='{$this->cookie}', ";
		$qr .= " session='{$this->session}', ";
		$qr .= " ip='{$this->ip}', ";
		$qr .= " signup_ip='{$this->signup_ip}', ";
		$qr .= " pref_lang='{$this->pref_lang}', ";
		$qr .= " hometown='{$this->hometown}', ";
		$qr .= " city='{$this->city}', ";
		$qr .= " state='{$this->state}', ";
		$qr .= " zip='{$this->zip}', ";
		$qr .= " theme='{$this->theme}', ";
		$qr .= " mobile='{$this->mobile}', ";
		$qr .= " usr_status='Ok' ";
	
		$qr .= " WHERE user_id={$this->user_id}";
		$res = mysql_query($qr);
	}
	
//	
}